
#include <boost/algorithm/string.hpp>
#include "WebKit.h"
#include "WebPage.h"
#include "WebElement.h"
#include "thread_spider.h"

#define MENPIAO_CSS "#js_nva_cgy > li.c_piao.js-searchnav > a"
#define MENPIAO_INPUT_CSS "#js_searchbox_piao > div.ch_sch_form.ch_sch_piao.clrfix > div.crl_group.ctl_group_input > div > div > div.qunar-qcbox > input"
#define MENPIAO_SERACE_CSS "#js_searchbox_piao > div.ch_sch_form.ch_sch_piao.clrfix > div.crl_group.crl_group_submit > div > p.p_btn > a"
#define MENPIAO_RESULT_CSS "#search-list > div:nth-child(1) > div > div.sight_item_about > h3 > a"
#define MENPIAO_LISTALL_CSS "#search-list > div:nth-child(1) > div > div.sight_in_cont > div > div.sight_in_more > a"
#define MENPIAO_LIST_RESULT_CSS "#ticket-list > div"

#define INPUTCSS "#search-content > div > form > input"
#define SERACECSS "#search-content > div > form > a"
#define RESULTCSS "body > div.ng-scope > div > div.row-fluid > div > div > font"

using namespace SpiderWebKitClient;
using namespace std;
using namespace boost::algorithm;

static int product_count = 0;

void collectTest()
{
    WebKit* wk = new WebKit("test");
    WebPage *page = wk->getWebPage("http://115.28.167.41:8002/test2");
    usleep(200000);

    std::list<WebElement> collection;
    collection = page->getElementsByName("fruit");
    std::cout << collection.size() << std::endl;

    WebElement upload = page->getElementById("upload");
    if (!upload.isValid())
        std::cout << "get upload element error." << std::endl;

    std::list<WebElement>::iterator it;
    for(it = collection.begin(); it != collection.end(); it ++) {
        (*it).setChecked(true);
        std::cout << (*it).getValue() << ":" << (*it).checked() << std::endl;
        upload.click();
        std::cout << page->getAlertText(false) << std::endl;
    }

    wk->destoryWebPage(page);
}

void collectQuNarMenpiao()
{
    WebKit* wk = new WebKit("test");

    WebPage *page = wk->getWebPage("http://www.qunar.com/");
    usleep(200000);

  while(1) {

	std::cout << page->getUrl() << std::endl;

	std::string place;
    std::cout << "Input:" << std::endl;
	std::cin >> place;

    usleep(1000000);

    std::list<WebElement> collection;
    int count = 0;
    int times = 0;
    while(count == 0) {
        if (times > 10)
            break;
        usleep(200000);
        collection = page->getElementsByCssSelector(MENPIAO_CSS);
        std::cout << "size:" << collection.size() << std::endl;
        count = collection.size();
        times ++;
    }

    WebElement piaoElement;
    if (collection.size() > 0)
        piaoElement = collection.front();
    else {
        std::cout << "get piao element failed." << std::endl;
        continue;
    }

    piaoElement.click();
    usleep(200000);

    collection = page->getElementsByCssSelector(MENPIAO_INPUT_CSS);

    WebElement inputElement;
    if (collection.size() > 0)
        inputElement = collection.front();
    else {
        std::cout << "get piao input element failed." << std::endl;
        continue;
    }

    collection = page->getElementsByCssSelector(MENPIAO_SERACE_CSS);
    WebElement searchElement;

    if (collection.size() > 0)
        searchElement = collection.front();
    else {
        std::cout << "get piao search element failed." << std::endl;
        continue;
    }

    inputElement.type(place);
    //searchElement.submit();
    inputElement.sendKeys("keypress", Key_Enter);

    count = 0;
    while(count == 0) {
        sleep(1);
        collection = page->getElementsByCssSelector(MENPIAO_RESULT_CSS);
        std::cout << "size:" << collection.size() << std::endl;
        count = collection.size();
    }

	std::cout << page->getUrl() << std::endl;

    std::cout << collection.front().getOuterXml() << std::endl;

    collection = page->getElementsByCssSelector(MENPIAO_LISTALL_CSS);

	if (collection.size() > 0)
		std::cout << collection.front().getOuterXml() << std::endl;

	page->goBack();
  }

    wk->destoryWebPage(page);
}

